package com.example.security.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.security.Security;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {

    @GetMapping("/")
    public Map getIndex() {

//        获取当前用户信息 context
        SecurityContext context = SecurityContextHolder.getContext();//存储认证对象的上下文
        Authentication authentication = context.getAuthentication(); // 获取认证对象
        String username = authentication.getName();//用户名
//        Object principal =authentication.getPrincipal();//身份
//        Object credentials = authentication.getCredentials();//凭证(脱敏)
//        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();//权限
        System.out.println(username);
//        System.out.println(principal);
//        System.out.println(credentials);
//        System.out.println(authorities); //创建结果对象
        HashMap result = new HashMap();
        result.put("code", 0);
        result.put("username", username);
        result.put("authentication", authentication);
        return result;

    }
}
